package ch.nostromo.tiffanys.game.chess.pieces;

import java.util.ArrayList;

import ch.nostromo.lib.util.NosTools;
import ch.nostromo.tiffanys.game.board.Board;
import ch.nostromo.tiffanys.game.board.BoardException;
import ch.nostromo.tiffanys.game.board.BoardTools;
import ch.nostromo.tiffanys.game.move.MoveInput;

public class King extends ChessPiece {

  public static ArrayList<MoveInput>  getMoveList(Board board, int currentPosition, int owner) throws BoardException {

    ArrayList<MoveInput> result = new ArrayList<MoveInput>();
    
    int toField = 0;


    toField = currentPosition + 1;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition -1;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition + 10;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition - 10;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition + 11;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition -11;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition -9;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    toField = currentPosition +9;
    if (board.isValidMoveToField(toField, owner)) {
      result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    // castling ... with check test !
    int longRook = 21;
    int shortRook = 28;
    int kingPos = 25;

    if (owner == Board.OWNER_BLACK) {
      longRook = 91;
      shortRook = 98;
      kingPos = 95;
    }

    // long castling
    if (currentPosition==kingPos && NosTools.checkMask(board.getBoardArray()[longRook], Board.PIECE_ROOK) && !board.isMovedField(longRook) && !board.isMovedField(currentPosition) && board.isEmptyField(longRook + 1) && board.isEmptyField(longRook + 2)  && board.isEmptyField(longRook + 3)) {
      // Check for check
      if (!ChessPiece.isKingAttacked(board, currentPosition, owner) && !ChessPiece.isKingAttacked(board, currentPosition -1, owner) && !ChessPiece.isKingAttacked(board, currentPosition -2, owner)) {
        result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(currentPosition - 2)));
      }
    }

    // short castling
    if (currentPosition==kingPos && NosTools.checkMask(board.getBoardArray()[shortRook], Board.PIECE_ROOK) && !board.isMovedField(shortRook) && !board.isMovedField(currentPosition)
        && board.isEmptyField(shortRook - 1) && board.isEmptyField(shortRook - 2)) {

      if (!ChessPiece.isKingAttacked(board, currentPosition, owner) && !ChessPiece.isKingAttacked(board, currentPosition + 1, owner) && !ChessPiece.isKingAttacked(board, currentPosition +2, owner)) {
        result.add( new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(currentPosition + 2)));
      }
    }

    return result;

  }



}